package router0

import (
	"net/http"
	db0 "searcher/src/db"

	"github.com/gin-gonic/gin"
	"github.com/shockerli/cvt"
)

func all_routers_for_search_tool(r *gin.Engine) {
	var rst RouterSearchTool
	rst.allMusictTypes(r)
	rst.queryByTitle(r)
	rst.queryByTitleAndType(r)
	rst.queryByMtype(r)
	rst.queryAllTitles(r)
	rst.increaseClick(r)
}

var (
	ref db0.DbHifiniRef
)

type RouterSearchTool struct{}

// 所有的type
func (*RouterSearchTool) allMusictTypes(r *gin.Engine) {
	url := "/api/searcher/musictype/all"
	r.GET(url, func(c *gin.Context) {
		res := ref.GetMusicType()

		c.JSON(http.StatusOK, res)
	})
}

// 根据歌名查询
func (*RouterSearchTool) queryByTitle(r *gin.Engine) {
	url := "/api/searcher/title/query/:page/:pagesize/:title"
	r.GET(url, func(c *gin.Context) {
		page := c.Param("page")
		pagesize := c.Param("pagesize")
		title := c.Param("title")
		upage := cvt.Uint(page)
		upagesize := cvt.Uint(pagesize)
		res := ref.QueryByTitle(upage, upagesize, title)

		c.JSON(http.StatusOK, res)
	})
}

// 根据歌名已经类型查询
func (*RouterSearchTool) queryByTitleAndType(r *gin.Engine) {
	// /?title=''
	url := "/api/searcher/title_and_type/query/:page/:pagesize/:type"
	r.GET(url, func(c *gin.Context) {
		page := c.Param("page")
		pagesize := c.Param("pagesize")
		_type := c.Param("type")
		title := c.Query("title")
		upage := cvt.Uint(page)
		upagesize := cvt.Uint(pagesize)
		res := ref.QueryByTitleAndType(upage, upagesize, title, _type)

		c.JSON(http.StatusOK, res)
	})
}

// 根据`music type`查询
func (*RouterSearchTool) queryByMtype(r *gin.Engine) {
	url := "/api/searcher/mtype/query/:page/:pagesize/:mtype"
	r.GET(url, func(c *gin.Context) {
		page := c.Param("page")
		pagesize := c.Param("pagesize")
		mtype := c.Param("mtype")
		upage := cvt.Uint(page)
		upagesize := cvt.Uint(pagesize)
		// 特殊处理 (250710)为空的数据只有四条,还是无效的数据,不再处理此类型
		if mtype == "kong" {
			mtype = ""
		}
		res := ref.QueryByMtype(upage, upagesize, mtype)

		c.JSON(http.StatusOK, res)
	})
}

// 模糊查询所有title
func (*RouterSearchTool) queryAllTitles(r *gin.Engine) {
	url := "/api/searcher/alltitles/query/:title"
	r.GET(url, func(c *gin.Context) {
		title := c.Param("title")
		res := ref.QueryAllTitles(title)

		c.JSON(http.StatusOK, res)
	})
}

// 增加点击
func (*RouterSearchTool) increaseClick(r *gin.Engine) {
	url := "/api/searcher/IncreaseClick/:id"
	r.POST(url, func(c *gin.Context) {
		id := c.Param("id")
		ref.IncreaseClick(cvt.Uint(id))

		c.JSON(http.StatusOK, true)
	})
}
